package studentmansger;

import java.util.Scanner;
public class StudentView {
    // 键盘录入
    Scanner sc = new Scanner(System.in);
    // 设置数组长度
    Student[] students = new Student[50];

    int count = 0;
    // 查询
    Student[] newStudents = new Student[students.length-1];


    public void start(){
        while (true){
            System.out.println("=================== 学生管理系统 ====================");
            System.out.println("1、添加学生");
            System.out.println("2、删除学生");
            System.out.println("3、修改学生");
            System.out.println("4、查看学生");
            System.out.println("5、学生详情");
            System.out.println("6、退出提醒");
            System.out.println("请选择（1-6）：");

            int num = sc.nextInt();
            System.out.println("------------------------------------------------------");
            switch (num){
                case 1 :
                    addStudents();
                    break;
                case 2 :
                    deleteStudents();
                    break;
                case 3 :
                    updateStudents();
                    break;
                case 4:
                    findAllStudents();
                    break;
                case 5 :
                    findStudents();
                    break;
                case 6 :
                    System.out.println("是否退出?按0为退出/按9为取消");
                    int key = sc.nextInt();
                    if(key == 0){
                        System.out.println("欢迎下次光临");
                        return;
                    } else if (key == 9) {
                        break;
                    }
                default:
                    break;
            }
        }
    }
    String tbody = "学号"+" \t "+"姓名"+" \t "+"年龄"+" \t "+"性别";
    private void findStudents() {
//        System.out.println("请输入你要查询的学生id");
//        int id = sc.nextInt();
//        int getIdIndex = ArrayUtils.findIndexById(students,id,count);
//        System.out.println(tbody);
//        System.out.println(students[getIdIndex].getId()+" \t\t "+students[getIdIndex].getName()+" \t "+students[getIdIndex].getAge()+" \t "+students[getIdIndex].getSex());
        System.out.println("请选择查询条件：");
        System.out.println("1、查询id");
        System.out.println("2、查询姓名");
        System.out.println("3、查询性别");
        System.out.println("4、修改年龄");
        int num = sc.nextInt();

        switch (num){
            case 1 :
                System.out.println("请输入你要查询的学生id");
                int id = sc.nextInt();
                int getIdIndex = ArrayUtils.findIndexById(students,id,count);
                System.out.println(tbody);
                System.out.println(students[getIdIndex].getId()+" \t\t "+students[getIdIndex].getName()+" \t "+students[getIdIndex].getAge()+" \t "+students[getIdIndex].getSex());
                break;
            case 2 :
                System.out.println("请输入你要查询的学生姓名：");
                String name = sc.next();
                getName(name);
                break;
            case 3 :
                System.out.println("请输入你要查询的学生年龄：");
                int age = sc.nextInt();
                getAge(age);
                break;
            case 4 :
                System.out.println("请输入你要查询的学生性别：");
                String sex = sc.next();
                getSex(sex);
                break;
            default:
                break;
        }
    }
    private  void getName(String name){
        int[] arr =  ArrayUtils.findIndexByName(students,name,count);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
            System.out.println(students[arr[i]].getId()+" \t\t "+students[arr[i]].getName()+" \t "+students[arr[i]].getAge()+" \t "+students[arr[i]].getSex());
        }
    }
    private  void getAge(int age){
        int[] arr =  ArrayUtils.findIndexByAge(students,age,count);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(students[arr[i]].getId()+" \t\t "+students[arr[i]].getName()+" \t "+students[arr[i]].getAge()+" \t "+students[arr[i]].getSex());
        }
    }
    private  void getSex(String sex){
        int[] arr =  ArrayUtils.findIndexBySex(students,sex,count);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(students[arr[i]].getId()+" \t\t "+students[arr[i]].getName()+" \t "+students[arr[i]].getAge()+" \t "+students[arr[i]].getSex());
        }
    }
    private void findAllStudents() {
        System.out.println(tbody);
        for (int i = 0; i < count-1; i++) {
            for (int j = 0; j < count-1-i; j++) {
                if(students[j].getId() > students[j+1].getId()){
                    Student temp = students[j];
                    students[j] = students[j+1];
                    students[j+1] = temp;
                }
            }
        }
        if (count == 0){
            System.out.println("现在班级没有学生，待开班......");
        } else {
            for (int i = 0; i < count; i++) {
                System.out.println(students[i].getId()+" \t\t "+students[i].getName()+" \t "+students[i].getAge()+" \t "+students[i].getSex());
            }
        }
    }

    private void updateStudents() {
        System.out.println("请您输入所要学生学号：");
        int id = sc.nextInt();
        int updateIndex = ArrayUtils.findIndexById(students,id,count);
        System.out.println("1、修改姓名");
        System.out.println("2、修改年龄");
        System.out.println("3、修改性别");
        System.out.println("4、全部修改");
        System.out.println("请选择（1-4）：");
        int num = sc.nextInt();
        switch (num){
            case 1 :
                System.out.println("请输入修改的姓名：");
                String name = sc.next();
                students[updateIndex].setName(name);
                break;
            case 2 :
                System.out.println("请输入修改的年龄：");
                int age = sc.nextInt();
                students[updateIndex].setAge(age);
                break;
            case 3 :
                System.out.println("请输入修改性别：");
                String sex = sc.next();
                students[updateIndex].setSex(sex);
                break;
            case 4 :
                System.out.println("请输入修改的姓名：");
                String Name = sc.next();
                System.out.println("请输入修改的年龄：");
                int Age = sc.nextInt();
                System.out.println("请输入修改性别：");
                String Sex = sc.next();
                Student  student = new Student(id,Name,Age,Sex);
                students[updateIndex] = student;
                break;
        }
        System.out.println("修改成功！！！");
    }

    private void deleteStudents() {
        System.out.println("请您输入要删除的学生学号：");
        int id = sc.nextInt();
        int removeIndex = ArrayUtils.findIndexById(students,id,count);
        if(removeIndex == -1) {
            System.out.println("您要删除学生学号不存在！！！");
            return;
        }
        System.arraycopy(students,0,newStudents,0, removeIndex);
        System.arraycopy(students,removeIndex+1,newStudents,removeIndex,students.length-removeIndex-1);

        students = newStudents;
        count--;
        System.out.println("删除成功！！！");
    }

    private void addStudents() {
        System.out.println("请您输入学生学号：");
        int id = sc.nextInt();
        int num = ArrayUtils.findIndexById(students,id,count);
        if(num != -1) {
            System.out.println("您输入学生学号有冲突！！！");
           return;
        }
        System.out.println("请您输入学生姓名：");
        String name = sc.next();
        System.out.println("请您输入学生年龄：");
        int age = sc.nextInt();
        System.out.println("请您输入学生性别：");
        String sex = sc.next();
        Student student = new Student(id,name,age,sex);
        students[count] = student;
        count++;
        System.out.println("添加成功");
    }
}
